1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| def cal_item_sim(user_click): """ :param user_click:dict, key userid value [itemid1,itemid2] :return: dict, key:itemid i, value dict, value_key itemid_j, value_value simcore """ co_appear = {} item_user_click_item = {} for user, itemlist in user_click.items(): for index_i in range(0, len(itemlist)): itemid_i = itemlist[index_i] item_user_click_item.setdefault(itemid_i, 0) item_user_click_item[itemid_i] += 1 for index_j in range(index_i + 1, len(itemlist)): itemid_j = itemlist[index_j] co_appear.setdefault(itemid_i, {}) co_appear[itemid_i].setdefault(itemid_j, 0) co_appear[itemid_i][itemid_j] += base_contribute_score()
co_appear.setdefault(itemid_j, {}) co_appear[itemid_j].setdefault(itemid_i, 0) co_appear[itemid_j][itemid_i] += base_contribute_score()
item_sim_score = {} item_sim_score_sorted = {} for itemid_i, relate_item in co_appear.items(): for itemid_j, co_time in relate_item.items(): sim_score = co_time / math.sqrt(item_user_click_item[itemid_i] * item_user_click_item[itemid_j]) item_sim_score.setdefault(itemid_i, {}) item_sim_score[itemid_i].setdefault(itemid_j, 0) item_sim_score[itemid_i][itemid_j] = sim_score for itemid in item_sim_score: item_sim_score_sorted[itemid] = sorted(item_sim_score[itemid].items(), key=operator.itemgetter(1), reverse=True)
return item_sim_score_sorted
|